<?php
/**
 * eXor simple "crypted" system
 *
 * This is a very simple class to encrypt / decrypt non-sensible information
 * but necessary in simple process to order and show  patterns into a
 * professional overview.  For more information about the alghoritm, visit
 * http://code.apperture.org/es/alghoritms/crypting/eXor:_encriptacion_para_informacion_no_sensible
 *
 * @category  crypt
 * @package   CORE-crypt
 * @author    Juan Manuel Lemus <juan.lemus@apperture.org>
 * @copyright (c) 2008 by Apperture, Inc 
 * @license   http://www.apperture.org/es/licenses/apperture/gnugpl-3.0
 * @version   CVS: $Id: exor.php,v 0.100 2008/03/06 12:11:00 cellog Exp $
 * @link      http://download.apperture.org/multi-purpose/core/crypt.tar.gz
 **/

/**
  * Encrypt a Specified String.
  *
  * The algorithm is very simple.  This, split the string var for each char,
  * convert to ascii code and every decimal number is converted to hexadecimal
  * code.  Then, the new string is joined.
  *
  * @param  string : string to encrypt
  * @param  int    : number of uplines (dashes)
  * @param  int    : number of callback to evite (--) - max: 4
  * @return string
  * @access public
  */
function exor_crypt_string($string, $upline = 1, $callback = 0)
{
  # Adjusting	
  $string .= " ";
    
  # Running alghoritm
  for ($i = 0; $i < strlen($string); $i++) {
    $asc  = dechex( ord( substr($string, $i, 1)  ) );
    $chr .= (strlen($asc) > 1) ? $asc : "0" . $asc;
  }

  # Converting to upper (very stylish appearance)
  $chr    = strtoupper( substr($chr, 0, strlen($chr) ) );
    
  # Randoming dashes
  $dashs  = array();
  for ($i = 0; $i < $upline; $i++) $dashs[$i] = rand(1, strlen($chr) - 1);
    
  # Sorting dash array for best performance
  sort($dashs);
    
  # Verifing "--" case
  for ($i = 1; $i < count($dashs); $i++) {
    if     ($dashs[$i - 1]       == $dashs[$i]) { $dashs[$i] = rand(1, strlen($chr)); $i--; }
    elseif (($dashs[$i - 1] - 1) == $dashs[$i]) { $dashs[$i] = rand(1, strlen($chr)); $i--; }
    elseif (($dashs[$i - 1] + 1) == $dashs[$i]) { $dashs[$i] = rand(1, strlen($chr)); $i--; }
  }

  # Resorting dash array    
  sort($dashs);
    
  # Writing dash mask
  for ($i = count($dashs) - 1; $i >= 0; $i--) $chr = substr($chr, 0, $dashs[$i]) . "-" . substr($chr, $dashs[$i], strlen($chr));
    
  # Last verification for "--" case
  if ((strpos($chr, "--") === false) && ($callback < 3)) {
    if (substr($chr, strlen( $chr ) - 1, 1) == "-") return substr($chr, 0, strlen($chr) - 1);

    return $chr;
  } else
    return exor_crypt_string($string, $upline, $callback++);
}

/**
 * Decrypt a specified string.
 *
 * The function is the inverse algorithm of the encrypt function.
 *
 * @param  string
 * @return string
 * @access public
 */
function exor_decrypt_string($string)
{
  # Quiting dashes marks
  $string = str_replace("-", "", $string);
    
  # Decrypting
  for ($i = 0; $i < strlen($string) - 2; $i+=2) $chr .= chr( hexdec( substr($string, $i, 2) ) );
      
  return $chr;
}

echo exor_crypt_string("poc-admin.main", "2");
?>
